package com.ruoyi.inventory.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.inventory.domain.Store;
import com.ruoyi.inventory.service.IStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 仓库Controller
 *
 * @author xwj
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/inventory/store")
@Api(tags = "仓库")
public class StoreController extends BaseController {
    @Autowired
    private IStoreService storeService;

    /**
     * 查询仓库列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Store store) {
        startPage();
        List<Store> list = storeService.selectStoreList(store);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Store store) {
        startOrderBy();
        List<Store> list = storeService.selectStoreList(store);
        return success(list);
    }


    /**
     * 导出仓库列表
     */
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Store store) {
        List<Store> list = storeService.selectStoreList(store);
        ExcelUtil<Store> util = new ExcelUtil<Store>(Store.class);
        util.exportExcel(response, list, "仓库数据");
    }

    /**
     * 获取仓库详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(storeService.selectStoreById(id));
    }

    /**
     * 新增仓库
     */
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Store store) {
        SysUser user = getLoginUser().getUser();
        if(store.getId() == null) {
            store.setCreateId(user.getUserId());
            store.setCreateTime(DateUtils.getNowDate());
            store.setCreateBy(user.getNickName());
            storeService.insertStore(store);
        } else {
            store.setUpdateId(user.getUserId());
            store.setUpdateTime(DateUtils.getNowDate());
            store.setUpdateBy(user.getNickName());
            storeService.updateStore(store);
        }

        return success(store);
    }

    /**
     * 修改仓库
     */
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Store store) {
        SysUser user = getLoginUser().getUser();
        store.setUpdateId(user.getUserId());
        store.setUpdateTime(DateUtils.getNowDate());
        store.setUpdateBy(user.getNickName());
        return success(storeService.updateStore(store));
    }

    /**
     * 删除仓库
     */
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(storeService.deleteStoreByIds(ids));
    }

    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Store store) {
        int num = storeService.delete(store);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Store>> queryPage(@RequestBody Store store) {
        Pages<Store> page = storeService.queryPage(store);
        return ResponseModel.of(page);
    }

}
